package com.ljxt.dt.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-04-26 15:19
 * @copyright 老九学堂
 */

public  class  ServletUtil {
	private static final String regex = "^\\d+$";

	/**
	 * 	 设置表单传入进来的值 并将转为对象
	 * @param request 传入request
	 * @param obj 传入转换后的对象
	 * @param <T>
	 * @return
	 */
	public static <T> T toObj(HttpServletRequest request, T obj) {

		BeanInfo beanInfo = null;
	   //得到传入进来的对象的所有属性
		try {
			beanInfo = Introspector.getBeanInfo(obj.getClass());
		} catch (IntrospectionException e) {
			e.printStackTrace();

		}
		//得到属性的所有参数
		PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
		//循环遍历
		for (PropertyDescriptor propertyDescriptor:propertyDescriptors) {
			//获取setter方法
			Method setMethod = propertyDescriptor.getWriteMethod();
			//获取setter传入的数据类型

			String  type = request.getParameter(propertyDescriptor.getName());

			if(type == null || type.isEmpty()){
				continue;
			}
			//转换数据类型
			Object value = convert(propertyDescriptor.getPropertyType(),type);
			try {
				setMethod.invoke(obj,value);
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return  obj;


	}



	
	/**
	 * 获取整数参数
	 * @param request 传入request
	 * @param name 传入要获取的值
	 * @return 返回一个对应的int类型的值,没有则返回0
	 */
	public static Integer getQueryInt(HttpServletRequest request,String name) {
		String returnName = request.getParameter(name);
		if(returnName == null) {
			return 0;
		}
		//判断是否是纯数字

		if(! returnName.matches(regex)) {
			return 0;
		}
		//将字符串转换数字
		return Integer.parseInt(returnName);
		
	}

	public static  void forward(HttpServletRequest request, HttpServletResponse response,String str,Object...params)  {
		String webString = "/WEB-INF/views/";
		if(params != null && params.length == 1){
			webString = webString+params[0]+"/";
		}

		try {

			request.getRequestDispatcher(webString+str+".jsp").forward(request,response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static  void redirect(HttpServletRequest request, HttpServletResponse response,String str) {

		ServletContext application = request.getServletContext();
		try {
			response.sendRedirect(application.getAttribute("ctx")+str+"html");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}




	private static Object  convert(Type type, String str){

		Object newObj = null;
		if(type.toString().equals(Integer.class.toString())){

            try {
                newObj = Integer.parseInt(str);
            }catch (Exception e){
                e.printStackTrace();
            }



		}else if(type.toString().equals(Byte.class.toString())){
			newObj = Byte.parseByte(str);
		}else if(type.toString().equals(Short.class.toString())){
			newObj = Short.parseShort(str);
		}else if(type.toString().equals(Long.class.toString())){
			newObj = Long.parseLong(str);
		}else if(type.toString().equals(Float.class.toString())){
			newObj = Float.parseFloat(str);
		}else if(type.toString().equals(Double.class.toString())){
			newObj = Double.parseDouble(str);
		}else if(type.toString().equals(Date.class.toString())){

			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = format.parse(str);
				newObj = date;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else  if(type.toString().equals(Boolean.class.toString())){
			newObj = Boolean.parseBoolean(str);
		}

		else{
			// 最后就按照 字符串处理
			newObj = str;
		}
		return newObj;
	}




	/**
	 * 判断当前请求是否为Ajax
	 * @param request
	 * @return
	 */
	public static boolean isAjaxRequest(HttpServletRequest request) {
		String header = request.getHeader("X-Requested-With");
		return !StringUtils.isEmpty(header) && "XMLHttpRequest".equals(header);
	}

}
